package com.javasm.adminapi.module.system.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.javasm.adminapi.module.system.dto.MenuTypeDTO;
import com.javasm.adminapi.module.system.entity.Permission;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;


import java.util.List;

/**
 * 权限表(Permission)表数据库访问层
 *
 * @author makejava
 * @since 2025-10-14 21:47:37
 */
public interface PermissionDao extends BaseMapper<Permission> {

    @Select("select p.* from role_permission as rp,permission as p where rp.permission_id=p.id and rp.role_id = #{id} and p.status=1")
    List<Permission> getByRid(Long id);

    @Select("update permission set status=#{status} where id=#{id}")
    void updateStatus(@Param("id") Long id, @Param("status") Integer status);

    @Select("\n" +
            "SELECT \n" +
            "    a.id AS api_id,        \n" +
            "    a.url AS api_url,     \n" +
            "    a.type AS api_type, \n" +
            "    rp.role_id,\n" +
            "    CASE \n" +
            "\t\t\t\tWHEN a.type = 0 THEN 1\n" +
            "        WHEN a.type = 1 THEN rp.`add`\n" +
            "        WHEN a.type = 2 THEN rp.`update`\n" +
            "        WHEN a.type = 3 THEN rp.`delete`\n" +
            "    END AS has_permission,\n" +
            "\t\t\tp.permission_url as code\n" +
            "FROM \n" +
            "    sys_permission a  \n" +
            "JOIN\n" +
            "\tsys_menu_permission as smp On smp.permission_id=a.id\n" +
            "JOIN \n" +
            "    role_permission rp ON smp.menu_id = rp.permission_id\n" +
            "JOIN\n" +
            "\t\tpermission p ON rp.permission_id=p.id\n" +
            "WHERE \n" +
            "    rp.role_id = 1\n" +
            "    AND (\n" +
            "\t\t\t\ta.type=0\n" +
            "        OR (a.type = 1 AND rp.`add` = 1)\n" +
            "        OR (a.type = 2 AND rp.`update` = 1)\n" +
            "        OR (a.type = 3 AND rp.`delete` = 1)\n" +
            "    );\n" +
            "\t\t")
    List<MenuTypeDTO> getMenuType(Long id);
}

